package sy4_Creational_Pattern.builder.myBuilder;

import lombok.Data;
@Data
class Product{   //产品角色
    //产品各部件的名称
    private String food;
    private String drink;
}

abstract class Builder  { //角色2：抽象建造者Builder
    public abstract void buildFood(String food);
    public abstract void buildDrink(String drink);
    public abstract Product buildProduct();
}

class ConcreteBuilder  extends Builder  { //角色3：具体建造者类ConcreteBuilder
    String food;
    String drink;

    @Override
    public void buildFood(String food) {
        this.food=food;
    }
    @Override
    public void buildDrink(String drink) {
        this.drink=drink;
    }
    @Override
    public Product buildProduct() { //方法返回值类型，体现了ConcreteBuilder对Product的依赖
        Product product = new Product();
        product.setFood(food);
        product.setDrink(drink);
        return product;
    }
}

class Director  { //角色4：指挥者类Director，聚合Builder对象，指挥产品的建造过程
    private Builder builder;  //聚合

    public Director(Builder builder) {  //构造器注入
        this.builder = builder;
    }
    public Product construct(String food, String drink) {  //建造
        //调用构建方法
        builder.buildFood(food);
        builder.buildDrink(drink);
        return builder.buildProduct();  //返回复杂产品
    }
}

public class Client { //客户端
    public static void main(String[] args) {
        Builder builder=new ConcreteBuilder();  //创建建造者

        //使用指挥者，客户端程序就不需要知道产品建造细节
        Director director=new Director(builder);  //创建指挥者
        Product product = director.construct("炸鸡", "可乐");
        System.out.println(product.toString());  //输出产品

        //如果不使用指挥者，客户端程序则需要知道产品构建细节
		//注释下面建造的两行代码，将输出空产品
		/*builder.buildFood("炸鸡");
		builder.buildDrink("可乐");

        Product product = builder.buildProduct();
		System.out.println(product.toString());*/
    }
}
